import cern
import jarray
from Matrix import Matrix
import java
import org.apache

def ttest(X,Y,alpha):
	try:
		x = X._M.toArray()
		y = Y._M.toArray()
		return org.apache.commons.math.stat.inference.TTestImpl().tTest(x[0],y[0],alpha)
	except:
		return org.apache.commons.math.stat.inference.TTestImpl().tTest(X,Y.toArray(),alpha)
def stderr(X):
	if isinstance(X,cern.colt.list.DoubleArrayList):
		meanX = cern.jet.stat.DoubleDescriptive.mean(X)
		se = cern.jet.stat.DoubleDescriptive.standardError(X.size(),cern.jet.stat.DoubleDescriptive.sampleVariance(X,meanX))
	else:
		se = None
	return se

def ztest(x,X):
	
	if isinstance(X,Matrix):
		X=X._M.toArray()
		X=X.viewRow(0)
		h = None
		alpha = 0.05
		ci = (None,None)
	else:
		X=X.toArray()
		h = None
		alpha = 0.05
		ci = (None,None)

	V = cern.colt.list.DoubleArrayList()
	for i in xrange(0,len(X)):
		V.add(float(X[i]))
	x = cern.jet.stat.Descriptive.mean(V)
	sstd = cern.jet.stat.Descriptive.sampleStandardDeviation(V.size(),cern.jet.stat.Descriptive.sampleVariance(V,x))
	z = (x - mu) / (sstd/ java.lang.Math.sqrt(V.size()))
	pr = cern.jet.stat.Probability.normal(mu,(std*std),z)
	pv = 1.0 - pr
	if pr < alpha:
		h = 1
	else:
		h = 0
	# get confidence intervals
	return (h,pr,ci)

